<script setup lang="ts">
import { Pod } from "@/views/domain/kubeHud/api/k8s/types";
import { onMounted, ref, watch } from "vue";
import { useK8SApi } from "@/views/domain/kubeHud/api/k8s/k8s";
import { objectToString } from "@/views/domain/kubeHud/utils";
import PodStatusIcon from "./PodStatusIcon.vue";
import KSpace from "../../components/KSpace.vue";
import KMentionText from "../../components/KMentionText.vue";
import RouterLinkBtn from "@/views/widgets/RouterLinkBtn/index.vue";
import { KubeHudRouterPath } from "@/views/domain/kubeHud/router";

const { podRepo } = useK8SApi();

type Prop = {
  pods?: Pod[];
  namespace?: string;
  labelSelector?: { [key: string]: any };
};

const props = defineProps<Prop>();

// pod 列表
const podList = ref<Pod[]>(props.pods || []);

const reloadList = async () => {
  if (props.pods && props.pods.length > 0) {
    podList.value = props.pods;
  } else if (props.namespace) {
    const { ok, data } = await podRepo.getPodList({
      namespace: props.namespace,
      labelSelector: objectToString(props.labelSelector)
    });

    if (ok) {
      podList.value = data.items;
    }
  }
};

const podStatus = (pod: Pod) => {
  if (pod.status.phase === "Running") {
    return "success";
  } else if (pod.status.phase === "Pending") {
    return "warning";
  } else if (pod.status.phase === "Succeeded") {
    return "success";
  } else if (pod.status.phase === "Failed") {
    return "danger";
  } else {
    return "info";
  }
};
const podStatusDesc = (pod: Pod) => {
  if (pod.status.phase === "Running") {
    return "运行中";
  } else if (pod.status.phase === "Pending") {
    return "等待中";
  } else if (pod.status.phase === "Succeeded") {
    // @ts-ignore
    return pod.status?.conditions?.last()?.reason;
  } else if (pod.status.phase === "Failed") {
    // @ts-ignore
    return pod.status?.conditions?.last()?.reason;
  }
};

watch(
  () => props.pods,
  () => {
    reloadList();
  }
);

watch(
  () => props.namespace,
  () => {
    reloadList();
  }
);

onMounted(() => {
  reloadList();
});
</script>

<template>
  <el-table :data="podList" table-layout="auto">
    <el-table-column width="64">
      <template #default="{ row }">
        <PodStatusIcon :pod="row" />
      </template>
    </el-table-column>
    <el-table-column prop="metadata.name" label="Name">
      <template #default="{ row }">
        <KSpace direction="column">
          <RouterLinkBtn
            class="font-bold"
            :to="KubeHudRouterPath.PodDetails"
            :label="row.metadata.name"
            :query="{
              namespace: row.metadata.namespace,
              name: row.metadata.name
            }"
          />
          <KMentionText :title="podStatusDesc(row)" :type="podStatus(row)" />
        </KSpace>
      </template>
    </el-table-column>

    <el-table-column prop="status.phase" label="状态">
      <template #default="{ row }">
        <KMentionText :title="podStatusDesc(row)" :type="podStatus(row)" />
      </template>
    </el-table-column>

    <el-table-column label="节点">
      <template #default="{ row }">
        <el-text>
          {{ row.spec?.nodeName }} ({{ row.status?.hostIP || "-" }})
        </el-text>
      </template>
    </el-table-column>
    <el-table-column prop="metadata.creationTimestamp" label="创建时间" />
  </el-table>
</template>
<style lang="scss" scoped></style>
